//
//  ViewController.m
//  Test
//
//  Created by lizq on 15/8/17.
//  Copyright (c) 2015年 lizq. All rights reserved.
//

#import "ViewController.h"
#import "RunMain.h"
#import "NSString+Extend.h"
#import "SerializeThread.h"
#import "CollectionViewController.h"
#import "CollectionLayout.h"

extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate> {
    
}
@property(nonatomic,strong)NSMutableArray *itemArray;
@property(nonatomic,strong)CollectionLayout *layout;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *weekLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor blackColor]];
    self.itemArray = [NSMutableArray arrayWithCapacity:1];
    [self factoryData];
    [self setup];
    [self.collectionView reloadData];
//    char buffer[80];
//    time_t dateTime;
//    time(&dateTime);//获得当前时间
//    struct tm * timeInfo = localtime(&dateTime);
//    strftime(buffer, 80, [@"%Y.%m.%d %H:%M:%S" UTF8String], timeInfo);
//    NSString *string = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
//    NSLog(@"string:%@",string);
    
    
    // Do any additional setup after loading the view, typically from a nib.
//    RunMain *main =   [[RunMain alloc] init];
//    [main testRunTime];
//    [self testVaList];
//    [self testRunTimer];
    
//    SerializeThread *obj = [[SerializeThread alloc] init];
//    [obj runSerialize];
    
   
    
//    [self.view addSubview:self.weekLabel];
}

- (UIButton *)weekLabel {
    if(!_weekLabel) {
        _weekLabel = [[UIButton alloc] init];
        [_weekLabel setFrame:CGRectMake(0, 0, 200, 100)];
        [_weekLabel setBackgroundColor:[UIColor redColor]];
        [_weekLabel addTarget:self action:@selector(clickWeek) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weekLabel;
}

- (void)clickWeek {
    CollectionViewController *vc = [[CollectionViewController alloc] init];
    [self.view addSubview:vc.view];
//    [self.navigationController pushViewController:vc animated:YES];
}


- (void)factoryData {
    for(NSInteger i = 0;i< 5 ; i++) {
        SectionObject *obj = [[SectionObject alloc] init];
        obj.bShow = YES;
        obj.count = 6;
        obj.name = [NSString stringWithFormat:@"我是第%@个Section",@(i)];
        [self.itemArray addObject:obj];
    }
}

- (void)setup {
    [self.view addSubview:self.collectionView];
}

- (UICollectionView *)collectionView {
    if(!_collectionView) {
        self.layout = [[CollectionLayout alloc] init];
        self.layout.itemSize = CGSizeMake(100, 50);
        self.layout.minimumLineSpacing = 10;
        self.layout.minimumInteritemSpacing = 10;
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);
        self.layout.footerReferenceSize = CGSizeMake(0, 0);
        self.layout.currentSetion = 0;
        CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:self.layout];
        _collectionView.backgroundColor= [UIColor blueColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        [_collectionView registerClass:[CollectionHeadCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([CollectionHeadCell class])];
        [_collectionView registerClass:[CollectionHeadCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([CollectionHeadCell class])];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    SectionObject *obj = [self.itemArray objectAtIndex:section];
    if(obj.bShow) {
        return obj.count;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor grayColor]];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CollectionHeadCell *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([CollectionHeadCell class]) forIndexPath:indexPath];
    SectionObject *obj = [self.itemArray objectAtIndex:indexPath.section];
    [cell setData:obj];
    cell.refreshBlock = ^{
        self.layout.currentSetion = indexPath.section;
        [self.collectionView  reloadData];
//        NSMutableArray *items = [NSMutableArray array];
//        for(NSInteger i =0;i< 6;i++) {
//            NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:indexPath.section];
//            [items addObject:index];
//        }
//        if(obj.bShow) {
//            [self.collectionView performBatchUpdates:^{
//                [self.collectionView insertItemsAtIndexPaths:items];
//            } completion:nil];
//        }else {
//            [self.collectionView performBatchUpdates:^{
//                [self.collectionView deleteItemsAtIndexPaths:items];
//            } completion:nil];
//        }
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   // 添加或删除
    [self openSectionIndexPath:indexPath];
}

- (void)openSectionIndexPath:(NSIndexPath *)indexPath {
    SectionObject *obj = [self.itemArray objectAtIndex:indexPath.section];
    if(obj.count < 10) {
        obj.count++;
        NSIndexPath *index = [NSIndexPath indexPathForItem:obj.count-1 inSection:indexPath.section];
        [self.collectionView performBatchUpdates:^{
            [self.collectionView insertItemsAtIndexPaths:@[index]];
        } completion:nil];
    }else {
        obj.count--;
         NSIndexPath *index = [NSIndexPath indexPathForItem:obj.count inSection:indexPath.section];
        [self.collectionView performBatchUpdates:^{
            [self.collectionView deleteItemsAtIndexPaths:@[index]];
        } completion:nil];
    }
}

- (void)testVaList {
    [self multiParameter:@"daf",@"1",@"2",@"3",@"4"];
}

- (void)multiParameter:(NSString *)title,... {
    NSLog(@"title:%@",title);
    va_list list;
    va_start(list, title);
    id obj;
    NSInteger count = 0;
    do{
        obj = va_arg(list, id);
        count++;
        if(obj) {
            NSLog(@"The %ldth is %@",count,obj);
        }else {
            NSLog(@"The parameters total : %ld",count);
        }
    }while(obj);
    va_end(list);
}

- (void)testRunTimer {
    CFTimeInterval st = CACurrentMediaTime();
    for(NSInteger i = 0; i < 10000;i++) {
        @autoreleasepool {
            NSMutableArray *items = [NSMutableArray array];
            for(NSInteger j = 0; j < 10000;j++) {
                [items addObject:@"abc"];
            }
        }
    }
    CFTimeInterval et = CACurrentMediaTime();
    NSLog(@"total Runtime:%g - %g = %g s",et,st,et - st);
    
    
    uint64_t t = dispatch_benchmark(1000, ^{
        @autoreleasepool {
            NSMutableArray *items = [NSMutableArray array];
            for(NSInteger j = 0; j < 10000;j++) {
                [items addObject:@"abc"];
            }
        }
    });
    NSLog(@"Runtimer: %llu ns",t);
}

@end
